<?php
if (!defined('BASEPATH'))
	exit ('No direct script access allowed');
class Tools {

	/*
	 * 产生随机色
	 */
	public function rand_color() {
		$d = '';
		for ($a = 1; $a < 7; $a++) { //采用#FFFFFF方法，
			$d .= dechex(rand(0, 15)); //累加随机的数据--dechex()将十进制改为十六进制
		}
		return $d;
	}
	/*
	 * 一天的小时数
	 */
	public function day_hour() {
		$day_hour = array (
			0 => array (
				"00:00",
				"02:00"
			),
			1 => array (
				"02:01",
				"04:00"
			),
			2 => array (
				"04:01",
				"06:00"
			),
			3 => array (
				"06:01",
				"08:00"
			),
			4 => array (
				"08:01",
				"10:00"
			),
			5 => array (
				"10:01",
				"12:00"
			),
			6 => array (
				"12:01",
				"14:00"
			),
			7 => array (
				"14:01",
				"16:00"
			),
			8 => array (
				"16:01",
				"18:00"
			),
			9 => array (
				"18:01",
				"20:00"
			),
			10 => array (
				"20:01",
				"22:00"
			),
			11 => array (
				"22:01",
				"24:00"
			)
		);
		return $day_hour;
	}

	/* 系统相关操作函数集 */

	//系统操作windows 对话框
	###########################################################
	# $p_msg : 	显示对话内容
	# $p_mode : 转向状态控制 close:关闭窗口 ,back:返回上级页面 ,别的字符为转向该字符URL
	###########################################################
	function showMessage($p_msg, $p_mode) {
		ob_start();
		if ($p_mode == "close")
			$cmd = "top.close();";
		else
			if ($p_mode == "back")
				$cmd = "history.go(-1)";
			else
				$cmd = "location.href = '" . $p_mode . "';";

		echo '<script language="javascript">
					<!--
						alert("' . $p_msg . '");' . $cmd . '
					-->
						</script>';
		ob_end_flush();
		die;
	}

	/*
	 * 代码风格（临时单一，后期加强）
	 */
	function get_code($wid,$uid) {
		$arr = array (
			'no' => 1,
			'name' => '样式一',
			'code' => '<script src="http://www.aruila.com/start.php?wid=' . $wid . '&uid=' . $uid . '" language="javascript"  type="text/javascript"></script>',
		);
		return $arr;
	}
	/**
	 * 一段时间的天数
	 */
	public function date_2_day($sdate, $edate) {
		return floor((strtotime($edate) - strtotime($sdate)) / 86400) + 1;
	}

	/**
	 * 获取默认设置，需传wid
	 */
	public function default_setting($wid=null)
	{
		return array('wId'=>$wid,'autoPFlag'=>0,'lmtTime'=>15,
					 'wuxiao_Radio'=>1,'close_24'=>3,'close_Hours'=>24,
					 'close_Times_Jinggao'=>3,'close_Times_Zuzhi'=>5,
					 'msgAlarm'=>'您的电脑因多次无效点击竞价广告，已被我司列入黑名单。如您再次重复点击竞价广告，我司将采取法律手段。');
	}

/**
 * utf-8、gb2312都支持的汉字截取函数
 *
 * @param string $string
 * @param integer $sublen
 * @param integer $start
 * @param string $code
 * @return string
 */

	public function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') {
	if ($code == 'UTF-8') {
		$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
		preg_match_all($pa, $string, $t_string);

		if (count($t_string[0]) - $start > $sublen)
			return join('', array_slice($t_string[0], $start, $sublen)) . "...";
		return join('', array_slice($t_string[0], $start, $sublen));
	} else {
		$start = $start * 2;
		$sublen = $sublen * 2;
		$strlen = strlen($string);
		$tmpstr = '';

		for ($i = 0; $i < $strlen; $i++) {
			if ($i >= $start && $i < ($start + $sublen)) {
				if (ord(substr($string, $i, 1)) > 129) {
					$tmpstr .= substr($string, $i, 2);
				} else {
					$tmpstr .= substr($string, $i, 1);
				}
			}
			if (ord(substr($string, $i, 1)) > 129)
				$i++;
		}
		if (strlen($tmpstr) < $strlen)
			$tmpstr .= "...";
		return $tmpstr;
	}
}
	/**
	*	网站类型
	*/
	public function websiteType()
	{
		$webType = array(
					'公司门户',
					'信息提供',
					'电子商务',
					'B2B交易'
					);
		return $webType;
	}

	/**
	 * url2Array
	 */
	public function url2Array($url = null) {
		$data_tmp = array ();
		if ($url)
			parse_str($url, $data_tmp);
		else
			parse_str($_SERVER["QUERY_STRING"], $data_tmp);
		return $data_tmp;
	}


	/**
	 * 解析搜索引擎过来的关键词
	 * 返回关键词
	 */
	 public function getKeywordBySearch($refer , $wid=null)
	 {
	 	$url = @str_replace('?','',strstr($refer,'?'));
	 	$url_arr = Tools::url2Array($refer);
		preg_match("/^(http:\/\/)?([^\/]+)/i",$refer, $matches);
		if(count($matches) > 0) {
			$host = $matches[2];
		}else{
			return;
		}
	 	$search_arr = array('baidu'=>'wd','google'=>'q','sogou'=>'query');
	 	foreach($search_arr as $key=>$val)
	 	{
	 		if(strrchr($host,$key))
	 		{
				$keyword = isset($url_arr[$val])?$url_arr[$val]:null;
				if($keyword)
				{
					$price = $this->redisinit->get('searcher_'.$wid.'_'.$key.'_'.$keyword);
		 			//$keyword = iconv_set_encoding($keyword, "UTF-8");
		 			return array('searchEngine'=>$key,'keyword'=>$keyword,'price'=>$price);
		 			break;
				}
	 		}
	 	}
	 	return null;
	 }
}




?>
